package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author ：HX
 * @date ：Created in 2019/12/10
 * @description ：菜单控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private UserService userService;
    @Reference
    private MenuService menuService;
//    查询获得菜单，动态展示菜单
    @RequestMapping("getMenu")
    public Result getMenu(String username){
        try{
            LinkedHashSet<Menu> menus=userService.getMenu(username);

            List<Map> list=new ArrayList<>();

            if (menus!=null&&menus.size()>0) {
//                    获得最大权限值
                int parentMax=0;
                for (Menu menu : menus) {
                    if (parentMax<menu.getPriority()){
                        parentMax=menu.getPriority();
                    }
                }
                for (int i = 1; i <= parentMax; i++) {
                    for (Menu menu : menus) {
                        Map map = new HashMap();
                        if (menu.getPriority()==i){
                            map.put("path", menu.getPath());
                            map.put("title", menu.getName());
                            map.put("icon", menu.getIcon());
                            List<Menu> childrenList = menu.getChildren();

                            List<Map> children = new ArrayList<>();
                            if (childrenList!=null&&childrenList.size()>0) {
                                //                    获得最大权限值
                                int childrenMax=0;
                                for (Menu menu2 : childrenList) {
                                    if (childrenMax < menu2.getPriority()) {
                                        childrenMax = menu2.getPriority();
                                    }
                                }
                                for (int j = 1; j <= childrenMax; j++) {
                                    for (Menu menu2 : childrenList) {
                                        Map map2 = new HashMap();
                                        if (menu2.getPriority()==j){
                                            map2.put("path", menu2.getPath());
                                            map2.put("title", menu2.getName());
                                            map2.put("linkUrl", menu2.getLinkUrl());
                                            map2.put("children", new ArrayList<>());
                                            children.add(map2);
                                        }
                                    }
                                }
                            }
                            map.put("children", children);
                            list.add(map);
                        }
                    }
                }
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

//    菜单的分页查询
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult=menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
//    添加
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false,re.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }
//    查询全部
    @RequestMapping("findALlParentMenu")
    public Result findALlParentMenu(){
        try{
            List<Menu> list=menuService.findALlParentMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
//    回显
    @RequestMapping("findById")
    public Result findById(Integer id){
        try{
            Menu menu=menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menu);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
//    编辑
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu){
        try{
            menuService.edit(menu);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false,re.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }
//    删除
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try{
            menuService.deleteById(id);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false,re.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }
    //    查询全部
    @RequestMapping("findAll")
    public Result findAll(){
        try{
            List<Menu> list=menuService.findAll();
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,list);
        }catch (RuntimeException re){
            re.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
